import {$post} from '~/tool/axios'
import {useConfig, useSessionToken, useSaveTokenType, useToken} from "~/store";
import $router from "~/router";

const $save_token_type = useSaveTokenType()
const $session_token = useSessionToken()
const $token = useToken()
const $config = useConfig()

const admin_api = 'Admin'

export const configListAction = async () => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/system/config`
}, true)
export const updateConfigAction = async (data) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/system/update`, data
}, true)
export const configAssetsListAction = async () => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/system/assets/list`
}, true)
export const changeNicknameAction = async (nickname) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/change/nickname`, data: {nickname}
}, true)
export const updateAdminNicknameAction = async (data) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/update/nickname`, data
}, true)
export const updateAdminPasswordAction = async (data) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/update/password`, data
}, true)
export const updateAdminStatusAction = async (data) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/update/status`, data
}, true)
export const updateAdminAuthIdAction = async (data) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/update/auth`, data
}, true)
export const getAdminListAction = async (data, page) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/list?page=${page}`, data
}, true)
export const getAdminInfoAction = async (id) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/get/info`, data: {id}
}, true)
export const deleteAdminAuthAction = async (ids) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/auth/delete`, data: {ids}
}, true)
export const deleteAdminAction = async (ids) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/delete`, data: {ids}
}, true)
export const changePasswordAction = async (data) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/change/password`, data
}, true)
export const createAdminAction = async (data) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/create`, data
}, true)
export const createAdminAuthAction = async (data) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/auth/create`, data
}, true)
export const updateAdminAuthAction = async (data) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/auth/update`, data
}, true)
export const getAdminAuthSelectListAction = async () => await $post({url: `${$config.value.api.url}/api/${admin_api}/auth/select`}, true)
export const getAuthAllListAction = async () => await $post({url: `${$config.value.api.url}/api/${admin_api}/auth/all`}, true)
export const getAuthListAction = async () => await $post({url: `${$config.value.api.url}/api/${admin_api}/auth/list`}, true)
export const getMenuAction = async () => await $post({url: `${$config.value.api.url}/api/${admin_api}/menu`}, true)
export const getInfoAction = async () => await $post({url: `${$config.value.api.url}/api/${admin_api}/info`}, true)
export const checkStatusAction = async () => await $post({url: `${$config.value.api.url}/api/${admin_api}/status`}, true)
export const loginAction = async (data) => await $post({
    url: `${$config.value.api.url}/api/${admin_api}/login`, data
}, true)
export const yo = async (data) => await $post({url: `${$config.value.api.url}/api/yo`, data}, true)

export const getHeaders = () => {
    let $token
    if ($save_token_type.value === 'local') {
        $token = useToken()
    } else {
        $token = useSessionToken()
    }
    return {
        'Authorization': 'Bearer ' + $token.value
    }
}
export const $image = (path) => {
    return `${$config.value.api.url}${path}`
}
export const $response = (res, then, next = false) => {
    if (res) {
        if (res.code === 100003) {
            $session_token.value = null
            $token.value = null
            if (next) {
                next('/login')
            } else {
                $router.push('/login')
            }
        }
        if (res.code !== 200) return window.$message().error(res.message)
        then()
    }
}